package cn.mesmile.juc.queue;

import javax.swing.plaf.metal.MetalIconFactory;
import java.util.concurrent.ArrayBlockingQueue;

/**
 * @author zb
 * @date 2021/8/17 17:20
 * @Description 队列
 */
public class BlockingQueueDemo {

    public static void main(String[] args)  {
        // 非公平锁
        ArrayBlockingQueue<Object> objects = new ArrayBlockingQueue<>(10);

        new Thread(()->{
            for (int i = 0; i < 30; i++) {
                try {
                    objects.put(i);
                    System.out.println(Thread.currentThread().getName()+" 放入数据----> "+ i);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        },"A").start();

        new Thread(()->{
            for (int i = 0; i < 10; i++) {
                try {
                    Thread.sleep(200);
                    Object o = objects.take();
                    System.out.println(Thread.currentThread().getName()+" 取出数据" + o);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        },"B").start();

        new Thread(()->{
            for (int i = 0; i < 10; i++) {
                try {
                    Thread.sleep(200);
                    Object o = objects.take();
                    System.out.println(Thread.currentThread().getName()+" 取出数据" + o);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        },"C").start();


    }


}
